package com.estore.managerImpl;

import java.util.ArrayList;
import java.util.List;

import com.estore.dao.Product_Dao;
import com.estore.entity.Brand;
import com.estore.entity.Categories;
import com.estore.entity.Product;
import com.estore.info.BrandInfo;
import com.estore.info.CatInfo;
import com.estore.info.ProductInfo;
import com.estore.info.SubCatInfo;
import com.estore.manager.ProductManager;

public class ProductManagerImpl implements ProductManager {

	private Product_Dao productDao;

	@Override
	public List<CatInfo> listCatInfo() {
		List<Categories> categories = productDao.listCategories();
		List<CatInfo> list = new ArrayList<CatInfo>();

		if (categories != null) {
			for (Categories record : categories) {
				list.add(new CatInfo(record.getCatID(), record.getCatName()));
			}
		}
		return list;
	}

	@Override
	public List<SubCatInfo> listSubCatInfo() {
		List<Categories> categories = productDao.listCategories();
		List<CatInfo> list = new ArrayList<CatInfo>();
		List<SubCatInfo> sublist = new ArrayList<SubCatInfo>();

		if (categories != null) {
			for (Categories record : categories) {
				list.add(new CatInfo(record.getCatID(), record.getCatName()));
				List<Categories> subcategories = productDao
						.listSubCategories(record.getCatID());
				List<CatInfo> abc = new ArrayList<CatInfo>();
				for (Categories record1 : subcategories) {
					abc.add(new CatInfo(record1.getCatID(), record1
							.getCatName()));
				}
				sublist.add(new SubCatInfo(record.getCatID(), record
						.getCatName(), abc));
			}
		}
		return sublist;
	}

	@Override
	public List<ProductInfo> getListProduct(int catID) {
		List<Product> listproduct = productDao.getListProduct(catID);
		List<ProductInfo> info = new ArrayList<ProductInfo>();
		if (listproduct != null) {
			for (Product record : listproduct) {
				// info.add(new ProductInfo(record.getProductID(), record
				// .getProductName()));
			}
		}
		return info;
	}

	@Override
	public List<ProductInfo> getListAllProduct() {
		List<Product> listproduct = productDao.getListAllProduct();
		List<ProductInfo> info = new ArrayList<ProductInfo>();
		if (listproduct != null) {
			// System.out.println("------------------------------");
			for (Product record : listproduct) {
				// System.out.println(record.getProductCode());
				// info.add(new ProductInfo(record.getProductID(), record
				// .getProductName()));
			}
			// System.out.println("------------------------------");
		}
		return info;
	}

	@Override
	public void addProduct(ProductInfo info) {
		productDao.addProduct(info);
	}

	@Override
	public void addBrand(String brandName, String website) {
		productDao.addBrand(brandName, website);
	}

	@Override
	public List<BrandInfo> getListBrand() {
		List<Brand> listBrand = productDao.getListBrand();
		List<BrandInfo> brandInfo = new ArrayList<BrandInfo>();
		if (listBrand != null) {
			for (Brand record : listBrand) {
				brandInfo.add(new BrandInfo(record.getBrandID(), record
						.getBrandName(), record.getWebsite()));
			}
			return brandInfo;
		} else
			return null;
	}

	@Override
	public boolean checkBrandName(String brandName) {
		if (productDao.checkBrand(brandName) == true) {
			return true;
		} else
			return false;
	}

	@Override
	public BrandInfo getBrandInfo(int brandID) {
		if (productDao.getBrand(brandID) != null) {
			Brand brand = productDao.getBrand(brandID);
			BrandInfo brandInfo = new BrandInfo(brand.getBrandID(),
					brand.getBrandName(), brand.getWebsite());
			return brandInfo;
		} else
			return null;
	}

	@Override
	public void updateBrand(int brandID, String brandName, String website) {
		productDao.updateBrand(brandID, brandName, website);
	}

	@Override
	public void insertCategories(CatInfo cat) {
		productDao.insertCategories(cat);
	}

	public Product_Dao getProductDao() {
		return productDao;
	}

	public void setProductDao(Product_Dao productDao) {
		this.productDao = productDao;
	}

	@Override
	public CatInfo getCategories(Integer catID) {
		Categories cat = productDao.getCategories(catID);
		CatInfo catInfo = new CatInfo(cat.getCatID(), cat.getCatName(),
				cat.getSubOf());
		return catInfo;
	}

	@Override
	public boolean checkCategories(String catName) {
		if (!productDao.checkCat(catName)) {
			return false;
		} else
			return true;
	}

	@Override
	public void updateCategories(CatInfo info) {
		productDao.updateCategories(info);
	}

	@Override
	public boolean checkProduct(String productName) {
		if (productDao.checkProduct(productName)) {
			return true;
		} else
			return false;
	}

	@Override
	public List<ProductInfo> getTop12NewProduct() {
		List<ProductInfo> listProduct = productDao.getTop12NewProduct();
		return listProduct;
	}
}
